package com.zxy.controller;


import com.zxy.common.result.Result;
import com.zxy.util.AliyunOSSOperator;
import com.zxy.util.OperLog;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.util.UUID;

@Slf4j
@RestController
@RequestMapping("/common")
public class UploadController {
    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Autowired
    private AliyunOSSOperator ossOperator;

    //图片上传
    @OperLog(title = "图片上传",businessType = 0)
    @PostMapping("/upload")
    public Result upload(MultipartFile file) throws Exception {
        String filename = file.getOriginalFilename();
        if (filename != null) {
            String name = filename.substring(filename.lastIndexOf("."));
            String newName = UUID.randomUUID() + name;
            String upload = ossOperator.upload(file.getBytes(), newName);
            String realname = "/"+upload;
            redisTemplate.opsForValue().set("filename",realname);
            return Result.success(realname);
        }
        return Result.error("上传失败");
    }
}

